package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.OrderDetailMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.*;
import com.itheima.service.DishService;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealDishService;
import com.itheima.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/14 21:49
 * @Desc:
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public Page<Orders> listAll(Integer page, Integer pageSize, String number, LocalDateTime beginTime, LocalDateTime endTime) {
        Page<Orders> page1 = new Page<>(page, pageSize);

        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        if (number != null)
            wrapper.eq("number", number);
        if (beginTime != null && endTime != null)
            wrapper.lt("order_id", beginTime).gt("order_id", endTime);
        wrapper.orderByDesc("order_time");

        return orderMapper.selectPage(page1, wrapper);
    }

    @Override
    public Page<Orders> listAll(Integer page, Integer pageSize) {
        Page<Orders> page1 = new Page<>(page, pageSize);

        return orderMapper.selectPage(page1, null);
    }

    @Override
    @Transactional
    public void submitOrder(Orders orders, HttpServletRequest request) {
        ShoppingCart shoppingCart = (ShoppingCart) request.getSession().getAttribute("shoppingCart");

        Long userId = BaseContext.getCurrentId();
        User user = userMapper.selectById(userId);

        String orderNumber = UUID.randomUUID().toString().replace("-", "");
        orders.setNumber(orderNumber);
        orders.setUserId(userId);
        orders.setPhone(user.getPhone());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(shoppingCart.getAmount());

        orderMapper.insert(orders);

        Long setmealId = shoppingCart.getSetmealId();
        List<SetmealDish> setmealDishes = setmealDishService.findBySetmealId(setmealId);

        if (setmealDishes.size() != 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                DishNew dishNew = dishService.findDishById(setmealDish.getDishId());

                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setName(dishNew.getName());
                orderDetail.setImage(dishNew.getImage());
                orderDetail.setOrderId(orders.getId());
                orderDetail.setDishId(dishNew.getId());
                orderDetail.setSetmealId(setmealDish.getSetmealId());
                orderDetail.setDishFlavor(dishNew.getFlavors().get(0).getName());
                orderDetail.setAmount(orders.getAmount());

                orderDetailMapper.insert(orderDetail);
            }
        } else {
            DishNew dishNew = dishService.findDishById(setmealId);

            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(dishNew.getName());
            orderDetail.setImage(dishNew.getImage());
            orderDetail.setOrderId(orders.getId());
            orderDetail.setDishId(dishNew.getId());
//            orderDetail.setDishFlavor(dishNew.getFlavors().get(0).toString());
            orderDetail.setAmount(orders.getAmount());

            orderDetailMapper.insert(orderDetail);
        }

        request.getSession().removeAttribute("shoppingCart");
        shoppingCartMapper.deleteById(shoppingCart);
    }
}
